######################################################################################
## Institutional Convergence in Europe								##
## Nina Sch�nfelder and Helmut Wagner								##
##  														##
## Program code to generate figures and estimate convergence in Section 5 		##
## Sigma and unconditional beta convergence							##
## PMR- und Distance to the Frontier-Indicators							##
## R version 3.2.2 (2015-08-14) on platform: x86_64-w64-mingw32/x64 (64-bit)		##
######################################################################################

######################################################################################
### Read and process data										##
######################################################################################

###################################################
###OECD Product Market Regulation

### Economy-wide Product Market Regulation (PMR)
## Original Excel-file is converted to cvs previously.
PMR<-read.csv2(file="PMR.csv", nrows=47,na.strings=".")
colnames(PMR)[1]<-"Country"

## Select sample: 
# "Old" EU members,without Luxembourg because of missing data in 1998.
EUold<-c("Austria","Belgium","Denmark","Finland","France", "Germany","Greece",
	"Ireland","Italy", "Netherlands","Portugal", "Spain", "Sweden", "United Kingdom")   

# "Old" EU members that introduced the euro first without Luxembourg because of missing data.
EAold<-c("Austria","Belgium","Finland","France", "Germany","Greece","Ireland","Italy", 
	"Netherlands","Portugal", "Spain") 

Aggregates_PMR<-list(EUold, EAold)


###################################################
###Ease of Doing Business:DTF 

### Doing Business Indicators
##  Original Excel-file is converted to cvs previously.
DB<-read.csv2(file="DB14-Distance-to-Frontier-dataset.csv", nrows=2200)
## Delete column X
DB<-DB[-15]
##Data in Doing Business Reports are current as of the prevoius year. 
DB$Year<-DB$DB.year-1

## Select countries which were at least potential candidate countries to the EU.
countries<-c("Belgium","Germany","Estonia","Ireland","Greece","Spain","France","Italy",
	"Cyprus","Luxembourg","Malta","Netherlands","Austria","Portugal","Slovenia",
	"Slovak Republic","Finland","Bulgaria","Czech Republic","Denmark","Latvia","Lithuania",
	"Hungary","Poland","Romania","Sweden","United Kingdom","Croatia","Macedonia, FYR",
	"Iceland","Montenegro","Serbia","Turkey","Albania", "Bosnia and Herzegovina", "Kosovo")

DB<-subset(DB,Economy %in% countries)
DB<-droplevels(DB)

## Work on the Doing Business dataframe
# Select the variables Economy, Year and DTF.overall.without.electricity from year=2005 on.
year<-c(2005:2013)
DB<-subset(DB,Year %in% year,select=c(Economy, Year, DTF.overall.without.electricity))
colnames(DB)<-c("Country", "Year","DTF")

# Reshapes the data frame between �long� format with the repeated measurements in separate records 
	and �wide� format with repeated measurements in separate columns of the same record.
     
DB<-reshape(DB,idvar="Country",timevar="Year",direction="wide")

EUaffiliates<-c("Belgium","Germany","Estonia","Ireland","Greece","Spain","France","Italy",
	"Netherlands","Austria","Portugal","Slovenia",
	"Slovak Republic","Finland","Bulgaria","Czech Republic","Denmark","Latvia","Lithuania",
	"Hungary","Poland","Romania","Sweden","United Kingdom","Croatia","Macedonia, FYR",
	"Iceland","Serbia","Turkey","Albania", "Bosnia and Herzegovina" )
 	# ohne Zypern, "MONTENEGRO" "KOSOVO", Malta, Luxembourg, da Daten f�r die ersten Jahre fehlen
EU27<-c("Belgium","Germany","Estonia","Ireland","Greece","Spain","France","Italy",
	"Netherlands","Austria","Portugal","Slovenia",
	"Slovak Republic","Finland","Bulgaria","Czech Republic","Denmark","Latvia","Lithuania",
	"Hungary","Poland","Romania","Sweden","United Kingdom")
	# ohne Zypern,Malta, Luxembourg
EA17<-c("Belgium","Germany","Estonia","Ireland","Greece","Spain","France","Italy",
	"Netherlands","Austria","Portugal","Slovenia",
	"Slovak Republic","Finland")
	# ohne Zypern,Malta, Luxembourg

EA12<-c("Belgium","Germany","Ireland","Greece","Spain","France","Italy",
	"Netherlands","Austria","Portugal","Finland")
	# Ohne Luxembourg

Aggregates_DB<-list(EUaffiliates, EU27, EA17, EA12)



######################################################################################
### Sigma convergence:											##
### Make cross-country variances for each year and two country groups               ##
######################################################################################

###################################################
###OECD Product Market Regulation

sigma<-sapply(1:2, function(x) apply(subset(PMR, subset= Country %in% Aggregates_PMR[[x]]),2,FUN=var, na.rm=FALSE)[-1])
years<-as.vector(c(1998,2003,2008,2013),mode="integer")
sigma<-as.data.frame(sigma,row.names=years)
colnames(sigma)<-c("EUold", "EAold")
Sub<-expression( sigma["PMR"]^2)


# Generate figures
win.metafile(filename = "PMR_DTF_sigma_conv.wmf",  width = 6.3, height = 3, pointsize=8, restoreConsole = FALSE)
par(mfrow=c(1,2),mar=c(3.5,4,2,1))

xmax<-max(sigma$EUold)

plot(rownames(sigma),sigma$EUold, type="o", xlab="Year", ylab=Sub,xaxp=c(1998,2013,3), 
	cex.main=1.05, main="Product market regulation", mgp=c(2.2, 1,0))
lines(rownames(sigma),sigma$EAold, type="o", pch=4);
legend(x="topright",legend=c("EUold","EA12") , lty=c(1,1),pch=c(1,4) ,bty="n")


###################################################
###Ease of Doing Business:DTF 

sigma<-sapply(1:4, function(x) apply(subset(DB, subset= Country %in% Aggregates_DB[[x]]),2,FUN=var, na.rm=FALSE)[-1])
years<-as.vector(c(2005:2013),mode="integer")
sigma<-as.data.frame(sigma,row.names=years)
colnames(sigma)<-c("EUaffiliates", "EU27", "EA17", "EA12")
Sub<-expression( sigma["DTF"]^2)

# Generate figures
plot(rownames(sigma),sigma$EUaffiliates, type="l", xlab="Year",ylim=c(40,110), ylab=Sub,
	, cex.main=1.05, main="Business regulation", mgp=c(2.2, 1,0))
lines(rownames(sigma),sigma$EU27,lty=2);
lines(rownames(sigma),sigma$EA17, type="o")
lines(rownames(sigma),sigma$EA12, type="o", pch=4)
legend(x="topright",legend=c("EU+aspirants","EU27", "EA17", "EA12"),
lty=c(1,2,1,1),pch=c(NA,NA,1,4) ,bty="n")

dev.off()



###################################################################################
### 		Beta convergence:									#####
###################################################################################
# Define vectors and lists.
z<-vector("list",6)
Sum<-vector("list",6)
r2<-vector(length=6)
Coef1<-vector(length=6)
Coef2<-vector(length=6)
Pval_Ftest<-vector(length=6)
R2<-vector(length=6)
Formula<-vector(length=6)
xsub<-vector("character")
ysub<-vector("character")
Title<-vector("character")
MM<-vector("list")

###################################################
###OECD Product Market Regulation

## Calculate difference of the PMR indicator between 2013 und 1998.
PMR$PMR_OVL_Diff<-PMR$PMR_OVL2013 - PMR$PMR_OVL1998

MM$EUold<-subset(PMR, subset= Country %in% EUold)
MM$EAold<-subset(PMR, subset= Country %in% EAold)

Aggr<-c("European Union (old Member States)","euro area (12 countries)")


### Generate figures: beta convergence
win.metafile(filename = "PMR_beta_conv.wmf",  width = 6.3, height = 3.1, pointsize=8, restoreConsole = FALSE)
par(mfrow=c(1,2),mar=c(3.5,4,2,1))

for(i in 1:2) {
 xsub[i]<-"PMR in 1998"
 ysub[i]<-"Change in PMR 1998�2013"
 Title[i]<-Aggr[i]

 plot(MM[[i]][["PMR_OVL1998"]],MM[[i]][["PMR_OVL_Diff"]],ylim=c(-1.3,-0.2), xlim=c(1.3,2.8),
  xlab=xsub[i], ylab=ysub[i],  main=Title[i], font.main=2,cex.main =1.05, mgp=c(2.2, 1,0))
 z[[i]]<-lm( PMR_OVL_Diff ~PMR_OVL1998, data=MM[[i]])
 abline(reg=z[[i]])
 
 Sum[[i]]<-summary(z[[i]])
 r2[i]<-getElement(Sum[[i]],"r.squared") 
 r2[i]<-round(r2[i],2) 
 Coef1[i]<-round(Sum[[i]][["coefficients"]][1],2) 
 Coef2[i]<-round(Sum[[i]][["coefficients"]][2],2) 
 Pval_Ftest[i]<-round(Sum[[i]][["coefficients"]][8],2) 
 
 legend("topright", legend = bquote(paste("slope coef." == .(Coef2[i]),",  ","p-value"== .(Pval_Ftest[i]),",  ", R^2== .(r2[i]) )),bty="n")
}
dev.off()


###################################################
## Ease of Doing Business:DTF    

# Calculate difference of the indicator between 2005 und 2013.
DB$DTF_Diff<-DB$DTF.2013 - DB$DTF.2005

MM<-vector("list")
MM$EUaffiliates<-subset(DB, subset= Country %in% EUaffiliates)
MM$EU27<-subset(DB, subset= Country %in% EU27)
MM$EA17<-subset(DB, subset= Country %in% EA17)
MM$EA12<-subset(DB, subset= Country %in% EA12)

Aggr<-c("European Union including its aspirants", "European Union", "euro area (17 countries)", "euro area (12 countries)")

### Generate figures: beta convergence
win.metafile(filename = "DTF_beta_conv.wmf",  width = 6.3, height = 6.2, pointsize=10, restoreConsole = FALSE)
par(mfrow=c(2,2),mar=c(3.5,4,2,1))

for(i in 1:4) {
 xsub[i]<-"DTF in 2005"
 ysub[i]<-"Change in DTF 2005�2013"
 Title[i]<-Aggr[i]

 plot(MM[[i]][["DTF.2005"]],MM[[i]][["DTF_Diff"]], xlim=c(50,85), ylim=c(0,20) , 
 xlab=xsub[i], ylab=ysub[i],  main=Title[i], font.main=2,cex.main =1,mgp=c(2.2, 1,0))
 z[[i]]<-lm( DTF_Diff ~DTF.2005, data=MM[[i]])
 abline(reg=z[[i]])
 
 Sum[[i]]<-summary(z[[i]])
 r2[i]<-getElement(Sum[[i]],"r.squared")
 r2[i]<-round(r2[i],2) 
 Coef1[i]<-round(Sum[[i]][["coefficients"]][1],2) 
 Coef2[i]<-round(Sum[[i]][["coefficients"]][2],2) 
 Pval_Ftest[i]<-round(Sum[[i]][["coefficients"]][8],2)
 
 legend("topright", legend = bquote(paste("slope coef." == .(Coef2[i]),",  ","p-value"== .(Pval_Ftest[i]),",  ", R^2== .(r2[i]) )),bty="n")
}
dev.off()





